class FileTransfer {
    sealed class TransferProgress {
        data class Progress(val percentage: Int) : TransferProgress()
        data class Complete(val file: File) : TransferProgress()
        data class Error(val message: String) : TransferProgress()
    }

    fun transferFile(
        file: File,
        outputStream: OutputStream,
        onProgress: (TransferProgress) -> Unit
    ) {
        try {
            val buffer = ByteArray(8192)
            val fileSize = file.length()
            var bytesTransferred = 0L
            
            FileInputStream(file).use { input ->
                var read: Int
                while (input.read(buffer).also { read = it } != -1) {
                    outputStream.write(buffer, 0, read)
                    bytesTransferred += read
                    val progress = ((bytesTransferred.toFloat() / fileSize) * 100).toInt()
                    onProgress(TransferProgress.Progress(progress))
                }
            }
            onProgress(TransferProgress.Complete(file))
        } catch (e: Exception) {
            onProgress(TransferProgress.Error(e.message ?: "���输失败"))
        }
    }
} 